1 /*
2 * Title: S/MIME Project
3 * Description: S/MIME email sending capabilities
4 * @Author Vladimir Radisic
5 * @Version 2.0.1
6 */
7
8
9 package org.webdocwf.util.smime.crypto;
10
11
12 import org.webdocwf.util.smime.exception.SMIMEException;
13 import org.webdocwf.util.smime.exception.ErrorStorage;
14 import java.security.PrivateKey;
15 import java.security.Signature;
16 import org.bouncycastle.jce.provider.JDKDigestSignature;
17
18
19 /***
20 * SigningProcessor class is used for signing input datas acording to given
21 * signature algorithm which (can be SHA1_WITH_RSA, MD2_WITH_RSA, MD5_WITH_RSA
22 * or SHA1_WITH_DSA).
23 */
24 public class SigningProcessor {
25
26 /***
27 * For creating signature values necessary information are: data for signing as byte
28 * array, type of signing algorithm, and private key for performing of asymmetric
29 * encryption.
30 * @param forSigning0 information for signing
31 * @param key0 private key (DSA or RSA depend on type of signing)
32 * @param signAlg0 type of signing algorithm (can be SHA1_WITH_RSA, MD2_WITH_RSA,
33 * MD5_WITH_RSA or SHA1_WITH_DSA).
34 * @exception SMIMEException caused by non SMIMEException which can be one of the
35 * following: InvalidKeyException, SignatureException, NoSuchProviderException,
36 * NoSuchAlgorithmException.
37 */
38 public static byte[] getSignature(byte[] forSigning0, PrivateKey key0, String signAlg0) throws SMIMEException {
39 byte[] sVal = null;
40
41 try {
42 if (signAlg0.equalsIgnoreCase("SHA1_WITH_RSA")) {
43 JDKDigestSignature.SHA1WithRSAEncryption jd = new JDKDigestSignature.SHA1WithRSAEncryption();
44
45 jd.initSign(key0);
46 jd.update(forSigning0);
47 sVal = jd.sign();
48 } else if (signAlg0.equalsIgnoreCase("SHA1_WITH_DSA")) {
49 Signature sig = Signature.getInstance("SHA1withDSA", "SUN");
50
51 sig.initSign(key0);
52 sig.update(forSigning0);
53 sVal = sig.sign();
54 } else if (signAlg0.equalsIgnoreCase("MD2_WITH_RSA")) {
55 JDKDigestSignature.MD2WithRSAEncryption jd = new JDKDigestSignature.MD2WithRSAEncryption();
56
57 jd.initSign(key0);
58 jd.update(forSigning0);
59 sVal = jd.sign();
60 } else if (signAlg0.equalsIgnoreCase("MD5_WITH_RSA")) {
61 JDKDigestSignature.MD5WithRSAEncryption jd = new JDKDigestSignature.MD5WithRSAEncryption();
62
63 jd.initSign(key0);
64 jd.update(forSigning0);
65 sVal = jd.sign();
66 }
67 } catch (Exception e) {
68 throw SMIMEException.getInstance("org.webdocwf.util.smime.crypto.SigningProcessor",
69 e, "getSignature");
70 }
71 return sVal;
72 }
73 }
74
This page was automatically generated by Maven